const app = getApp()
const self=this
const request = (config) => {
	config.url = app.globalData.url + config.url;
	if (!config.data) {
		config.data = {};
	}
	if(uni.getStorageSync('Authorization')){
		app.globalData.hasToken = true
		config.header = {
			 'Authorization': 'Bearer ' + uni.getStorageSync('Authorization')
		}
		app.globalData.uploadheader={
			'Authorization': 'Bearer ' + uni.getStorageSync('Authorization')
		};
		
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(config).then(responses => {
			if (responses[0]) {
				reject({
					message: "网络超时"
				});
			} else {
				let response = responses[1].data; // 如果返回的结果是data.data的，嫌麻烦可以用这个，return res,这样只返回一个data
				 if (response.errorCode == '401' ) {
					uni.navigateTo({
						url: '../index/index'
					})
					
					uni.removeStorageSync('Authorization')
					app.globalData.hasToken = false
					//#ifdef H5
					 uni.clearStorageSync()
					//#endif
				}
				if (response.errorCode == '40009') {
					uni.clearStorageSync()
					uni.reLaunch({
						url: '/pages/index/index.vue'
					})
				} else {
					resolve(response);
				}
			}
		}).catch(error => {
			reject(error);
		})
	})
	return promise;
};

export default request
